﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 沙驰订货会删单打印 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 沙驰订货会删单打印()
        {
            InitializeComponent();
        }
        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();
                string sqldm = @"exec [zdy_proc_GetDelOrderByProduct_DL] '{0}'";
                sqldm = string.Format(sqldm, dm);
                DataTable dtdm = DBHelper_DHH.ExecuteDataTable(sqldm);
                if (dtdm.Rows.Count > 0)
                {
                    NewUsb.FindUSBPrinter();
                    for (int k = 0; k < NewUsb.USBPortCount; k++)
                    {
                        if (NewUsb.LinkUSB(k))
                        {
                            SendData2USB("SIZE 60 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                            SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                            SendData2USB("CLS\r\n");//清空缓冲区
                            SendData2USB("DENSITY 7\r\n");//打印浓度
                            SendData2USB("REFERENCE 0,0\r\n");

                            SendData2USB("TEXT 10,10,\"TSS24.BF2\",0,1,1,\"品名:\"\r\n");
                            SendData2USB("TEXT 80,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["品名"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,50,\"TSS24.BF2\",0,1,1,\"货号:\"\r\n");
                            SendData2USB("TEXT 80,50,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["款号"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,90,\"TSS24.BF2\",0,1,1,\"价格:\"\r\n");
                            SendData2USB("TEXT 80,90,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["价格"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 150,90,\"TSS24.BF2\",0,1,1,\"系列:\"\r\n");
                            SendData2USB("TEXT 210,90,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["系列"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 300,90,\"TSS24.BF2\",0,1,1,\"系列排名:\"\r\n");
                            SendData2USB("TEXT 410,90,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["系列排名"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,130,\"TSS24.BF2\",0,1,1,\"提前下单:\"\r\n");
                            SendData2USB("TEXT 120,130,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["提前下单"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 170,130,\"TSS24.BF2\",0,1,1,\"现订量:\"\r\n");
                            SendData2USB("TEXT 260,130,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["现订量"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 320,130,\"TSS24.BF2\",0,1,1,\"预估量:\"\r\n");
                            SendData2USB("TEXT 400,130,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["预估量"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,170,\"TSS24.BF2\",0,1,1,\"库存:\"\r\n");
                            SendData2USB("TEXT 120,170,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["库存"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,250,\"TSS24.BF2\",0,1,1,\"乌鲁木齐:\"\r\n");
                            SendData2USB("TEXT 120,250,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["乌鲁木齐"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,250,\"TSS24.BF2\",0,1,1,\"东营:\"\r\n");
                            SendData2USB("TEXT 260,250,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["东营"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,290,\"TSS24.BF2\",0,1,1,\"武汉:\"\r\n");
                            SendData2USB("TEXT 80,290,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["武汉"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,290,\"TSS24.BF2\",0,1,1,\"合肥:\"\r\n");
                            SendData2USB("TEXT 260,290,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["合肥"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,330,\"TSS24.BF2\",0,1,1,\"南通:\"\r\n");
                            SendData2USB("TEXT 80,330,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["南通"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,330,\"TSS24.BF2\",0,1,1,\"徐州:\"\r\n");
                            SendData2USB("TEXT 260,330,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["徐州"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,370,\"TSS24.BF2\",0,1,1,\"邯郸:\"\r\n");
                            SendData2USB("TEXT 80,370,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["邯郸"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,370,\"TSS24.BF2\",0,1,1,\"廊坊:\"\r\n");
                            SendData2USB("TEXT 260,370,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["廊坊"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,410,\"TSS24.BF2\",0,1,1,\"襄阳:\"\r\n");
                            SendData2USB("TEXT 80,410,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["襄阳"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,410,\"TSS24.BF2\",0,1,1,\"盐城:\"\r\n");
                            SendData2USB("TEXT 260,410,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["盐城"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,450,\"TSS24.BF2\",0,1,1,\"如皋:\"\r\n");
                            SendData2USB("TEXT 80,450,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["如皋"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,450,\"TSS24.BF2\",0,1,1,\"南宁:\"\r\n");
                            SendData2USB("TEXT 260,450,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["南宁"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,490,\"TSS24.BF2\",0,1,1,\"兰州:\"\r\n");
                            SendData2USB("TEXT 80,490,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["兰州"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,490,\"TSS24.BF2\",0,1,1,\"扬州:\"\r\n");
                            SendData2USB("TEXT 260,490,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["扬州"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,530,\"TSS24.BF2\",0,1,1,\"邢台:\"\r\n");
                            SendData2USB("TEXT 80,530,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["邢台"].ToString() + "\"\r\n");
        

                            SendData2USB("PRINT 1\r\n");
                            NewUsb.CloseUSBPort();

                            textBox1.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("请确定是否连接上打印机");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("款号不正确");
                }
            }
        }
    }
}
